/*
 * MainTabbedPane.java
 *
 * Created on December 2, 2007, 3:02 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package filecopier.gui;

import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import filecopier.gui.tabs.CopyPanel;
import filecopier.gui.tabs.RestorePanel;
import filecopier.gui.tabs.FileExistOptionsPanel;
import filecopier.gui.tabs.SourceDirectoryOptionsPanel;
import filecopier.gui.tabs.MiscellaneousOptionsPanel;
import filecopier.options.Options;
import filecopier.Singleton;
import filecopier.ActionCommand;

/**
 *
 * @author david
 */
public class MainTabbedPane extends JTabbedPane {

    private JPanel panels[] = new JPanel[Options.MISC_IDX + 1];

    /** Creates a new instance of MainTabbedPane */
    public MainTabbedPane() {
        super();
        initialize();
    }

    void initialize() {
        this.addTab("Copy", getCopyPanel());
        this.setMnemonicAt(Options.COPY_IDX, java.awt.event.KeyEvent.VK_P);
        this.addTab("Restore", getRestorePanel());
        this.setMnemonicAt(Options.RESTORE_IDX, java.awt.event.KeyEvent.VK_E);
        this.addTab("Existing Files", getFileExistOptionsPanel());
        this.setMnemonicAt(Options.EXISTINGFILES_IDX, java.awt.event.KeyEvent.VK_F);
        this.addTab("Source Directories", getSourceDirectoryOptionsPanel());
        this.setMnemonicAt(Options.SRCDIRS_IDX, java.awt.event.KeyEvent.VK_D);
        this.addTab("Miscellaneous", getMiscellaneousOptionsPanel());
        this.setMnemonicAt(Options.MISC_IDX, java.awt.event.KeyEvent.VK_M);
        this.setSelectedIndex(Singleton.getInstance().getOptions().getTabIndex().ordinal());
        this.addChangeListener(new javax.swing.event.ChangeListener() {

            public void stateChanged(javax.swing.event.ChangeEvent e) {
                Singleton.getInstance().getOptions().setTabIndex(getTabIndex());
            }
        });
    }

    private Options.TabIndex getTabIndex() {
        Options.TabIndex tabIndex = null;
        int idx = getSelectedIndex();
        if (idx == Options.TabIndex.Copy.ordinal()) {
            tabIndex = Options.TabIndex.Copy;
        } else if (idx == Options.TabIndex.ExistingFiles.ordinal()) {
            tabIndex = Options.TabIndex.ExistingFiles;
        } else if (idx == Options.TabIndex.Miscellaneous.ordinal()) {
            tabIndex = Options.TabIndex.Miscellaneous;
        } else if (idx == Options.TabIndex.Restore.ordinal()) {
            tabIndex = Options.TabIndex.Restore;
        } else {
            tabIndex = Options.TabIndex.SrcDirs;
        }

        return tabIndex;
    }

    public ActionCommand getActionCommand(int idx) {
        return (ActionCommand) panels[idx];
    }

    public JPanel getCopyPanel() {
        if (panels[Options.COPY_IDX] == null) {
            panels[Options.COPY_IDX] = new CopyPanel();
        }
        return panels[Options.COPY_IDX];
    }

    public JPanel getRestorePanel() {
        if (panels[Options.RESTORE_IDX] == null) {
            panels[Options.RESTORE_IDX] = new RestorePanel();
        }
        return panels[Options.RESTORE_IDX];
    }

    public JPanel getFileExistOptionsPanel() {
        if (panels[Options.EXISTINGFILES_IDX] == null) {
            panels[Options.EXISTINGFILES_IDX] = new FileExistOptionsPanel();
        }
        return panels[Options.EXISTINGFILES_IDX];
    }

    public JPanel getSourceDirectoryOptionsPanel() {
        if (panels[Options.SRCDIRS_IDX] == null) {
            panels[Options.SRCDIRS_IDX] = new SourceDirectoryOptionsPanel();
        }
        return panels[Options.SRCDIRS_IDX];
    }

    public JPanel getMiscellaneousOptionsPanel() {
        if (panels[Options.MISC_IDX] == null) {
            panels[Options.MISC_IDX] = new MiscellaneousOptionsPanel();
        }
        return panels[Options.MISC_IDX];
    }
}
